﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class MyCustomSort : MonoBehaviour
{
    public enum SortType
    {
        quality = 0,
        cost = 1,
        all=2,
        id = 3,
        none = 4,
    };
    public GameObject Root;
    public UIGrid grid;
    public GameObject m_sort;
    public UILabel sortLabel;
    List<Item> items = new List<Item>();

    SortType sortType = SortType.none;
    int sortIndex = 0;
    // Use this for initialization
    void Start()
    {
        for (int i = 0; i < Root.transform.childCount; i++)
        {
            items.Add(Root.transform.GetChild(i).GetComponent<Item>());
        }
        grid.sorting = UIGrid.Sorting.Custom;
        grid.onCustomSort = CustomSort;
        UIEventListener.Get(m_sort).onClick = OnObjBtnSortClick;
    }

    void OnObjBtnSortClick(GameObject go)
    {
        sortIndex++;
        int newSortIndex = sortIndex % 5;
        sortType = (SortType)Enum.ToObject(typeof(SortType), newSortIndex);

        items.Sort(delegate (Item a, Item b) { return CustomSort(a.transform, b.transform); });
        switch (sortType)
        {
            case SortType.quality:
                sortLabel.text = "按品质排序";
                break;
            case SortType.cost:
                sortLabel.text = "按价格排序";
                break;
            case SortType.all:
                sortLabel.text = "按综合排序";
                break;
            case SortType.id:
                sortLabel.text = "按ID排序";
                break;
            case SortType.none:
                sortLabel.text = "默认排序";
                break;
        }
        Debug.Log(sortIndex + " " + sortType + " " + sortLabel.text);

        foreach (var item in items)
        {
            Debug.Log("Name:"+item.ItemName+"Price:"+item.Price+" ID:"+ item.ID + " Quality:" + item.Quality);
            item.SetData(item);
        }

        //列表添加后用于刷新listView
        grid.repositionNow = true;
        //重设uigrid
        grid.Reposition();
    }

    int CustomSort(Transform t1, Transform t2)
    {
        Item a = t1.GetComponent<Item>();
        Item b = t2.GetComponent<Item>();

        switch (sortType)
        {
            case SortType.none:
                return 0;
            case SortType.quality:
                return a.Quality.CompareTo(b.Quality);
            case SortType.cost:
                return b.Price.CompareTo(a.Price);
            case SortType.all:
                int result = 0;
                result = a.ID.CompareTo(b.ID);
                if (result != 0)
                    return result;
                result = b.Price.CompareTo(a.Price);
                if (result != 0)
                    return result;
                result = a.Quality.CompareTo(b.Quality);
                if(result!=0)
                    return result;
                return result;
            case SortType.id:
                return a.ID.CompareTo(b.ID);
            default:
                return 0;
        }
    }
}
